package com.example.fengyungo.dao;

import com.example.fengyungo.domain.pojo.DriverMessageList;
import com.example.fengyungo.domain.pojo.Message;
import com.example.fengyungo.domain.pojo.UserMessageList;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface MessageDao {
    /**
     * 根据司机id获取消息列表
     *
     * @param driverId 司机id
     * @return 司机消息列表中的所有用户简单信息
     */
    @Select("select * from driver_message_list where driverId = #{driverId} and driverListStatus = 0")
    List<DriverMessageList> getMessageListByDriverId(String driverId);

    /**
     * 根据司机id和用户id获取聊天记录
     *
     * @param driverId 司机id
     * @param userId   用户id
     * @param num      聊天记录条数
     * @return 司机和用户的聊天记录
     */
    @Select("select * from message where receiverId=#{driverId} and senderId=#{userId} or receiverId=#{userId} and senderId=#{driverId} order by sendTime desc limit #{num}")
    List<Message> getMessagesByDriverIdAndUserId(@Param("driverId") String driverId, @Param("userId") String userId, @Param("num") int num);

    /**
     * 更新消息列表中的最新消息、未读消息数以及消息发送时间
     *
     * @param message Message
     * @return Integer
     */
    @Update("update driver_message_list set lastContent = #{content},lastTime=#{sendTime} where driverId=#{receiverId} and userId=#{senderId} or userId=#{receiverId} and driverId=#{senderId}")
    Integer updateDriverLastContentAndTime(Message message);

    /**
     * 插入消息到数据库中
     *
     * @param message 消息
     * @return Integer
     */
    @Insert("insert into message (messageId,senderId,receiverId,content,sendTime) VALUES (#{messageId},#{senderId},#{receiverId},#{content},#{sendTime})")
    Integer insertMessage(Message message);

    /**
     * 更新司机消息列表中的未读消息数
     *
     * @param driverId     司机id
     * @param userId       用户id
     * @param driverUnread 司机未读数
     * @return Integer
     */
    @Update("update driver_message_list set driverUnread=#{driverUnread} where driverId=#{driverId} and userId=#{userId}")
    Integer updateDriverUnread(@Param("driverId") String driverId, @Param("userId") String userId, @Param("driverUnread") Integer driverUnread);

    /**
     * 根据用户id获取用户的消息列表
     *
     * @param userId 用户id
     * @return 用户消息列表
     */
    @Select("select * from user_message_list where userId = #{userId} and userListStatus = 0")
    List<UserMessageList> getMessageListByUserId(String userId);

    /**
     * 更新用户消息列表中的最新消息以及消息发送时间
     *
     * @param message Message
     * @return Integer
     */
    @Update("update user_message_list set lastContent = #{content},lastTime=#{sendTime} where driverId=#{receiverId} and userId=#{senderId} or userId=#{receiverId} and driverId=#{senderId}")
    Integer updateUserLastContentAndTime(Message message);

    /**
     * 修改用户未读消息数
     *
     * @param userId     用户id
     * @param driverId   司机id
     * @param userUnread 未读消息数
     * @return Integer
     */
    @Update("update user_message_list set userUnread = #{userUnread} where userId = #{userId} and driverId = #{driverId}")
    Integer updateUserUnread(@Param("userId") String userId, @Param("driverId") String driverId, @Param("userUnread") Integer userUnread);

    /**
     * 获取司机未读消息数
     *
     * @param driverId 司机id
     * @param userId   用户id
     * @return Integer
     */
    @Select("select driverUnread from driver_message_list where driverId = #{driverId} and userId = #{userId}")
    Integer getDriverUnread(@Param("driverId") String driverId, @Param("userId") String userId);

    /**
     * 获取用户未读消息数
     *
     * @param userId   用户id
     * @param driverId 司机id
     * @return Integer
     */
    @Select("select userUnread from user_message_list where userId = #{userId} and driverId = #{driverId}")
    Integer getUserUnread(@Param("userId") String userId, @Param("driverId") String driverId);

    /**
     * 司机消息列表添加聊天
     * @param driverMessageList DriverMessageList
     * @return Integer
     */
    @Insert("insert into driver_message_list " +
            "(listId,driverId,userId,userNickname,userAvatar,userOnline,lastContent,lastTime,driverUnread,driverListStatus) " +
            "VALUES " +
            "(#{listId},#{driverId},#{userId},#{userNickname},#{userAvatar},#{userOnline},#{lastContent},#{lastTime},#{driverUnread},#{driverListStatus})")
    Integer addDriverMessageList(DriverMessageList driverMessageList);

    /**
     * 用户消息列表添加聊天
     * @param userMessageList UserMessageList
     * @return Integer
     */
    @Insert("insert into user_message_list " +
            "(listId, driverNickname, driverAvatar, driverOnline, userUnread, userId, driverId, lastContent, lastTime, userListStatus) " +
            "VALUES " +
            "(#{listId}, #{driverNickname}, #{driverAvatar}, #{driverOnline}, #{userUnread}, #{userId}, #{driverId}, #{lastContent}, #{lastTime}, #{userListStatus})")
    Integer addUserMessageList(UserMessageList userMessageList);

    /**
     * 修改司机消息列表状态
     * @param driverId 司机id
     * @param userId 用户id
     * @param driverListStatus 列表状态，0存在，1不存在
     * @return Integer
     */
    @Update("update driver_message_list set driverListStatus = #{driverListStatus} " +
            "where driverId = #{driverId} and userId = #{userId}")
    Integer updateDriverMessageListStatus(@Param("driverId") String driverId, @Param("userId") String userId, @Param("driverListStatus") Integer driverListStatus);

    /**
     * 修改用户消息列表状态
     * @param driverId 司机id
     * @param userId 用户id
     * @param userListStatus 列表状态，0存在，1不存在
     * @return Integer
     */
    @Update("update user_message_list set userListStatus = #{userListStatus} " +
            "where driverId = #{driverId} and userId = #{userId}")
    Integer updateUserMessageListStatus(@Param("driverId") String driverId, @Param("userId") String userId, @Param("userListStatus") Integer userListStatus);
}
